<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form onsubmit="return false">
    <textarea name="message" style="height:300px;width:300px"></textarea>
    <input type="button" value="发送消息" onclick="send(this.form.message.value)">
    <textarea id="responseText" style="height: 300px;width: 300px;"></textarea>
    <input type="button" value="清空内容" onclick="document.getElementById('responseText').value='';">
</form>
</body>
<script>
    var socket;
    var rt = document.getElementById('responseText');
    if (window.WebSocket) {
        socket = new WebSocket('ws://localhost:8080/mvc/hello');
        socket.onmessage = function (ev) {
            rt.value = rt.value + '\n' + ev.data;
        };
        socket.onopen = function (ev) {
            rt.value = '连接开启了';
        };
        socket.onclose = function (ev) {
            rt.value = rt.value + '\n连接关闭了...';
        };
    } else {
        alert('浏览器不支持webSocket');
    }

    function send(message) {
        if (!window.socket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alter('连接没有开启');
        }
    }

</script>
</html>